
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Item(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    def __repr__(self):
        return f"<Item {self.name}>"
with app.app_context():
    db.create_all()
@app.route('/')
def index():
    items = Item.query.all()
    return render_template('index.html', data_list=items)
@app.route('/add', methods=['POST'])
def add():
    new_item = request.form.get('new_item')
    if new_item:
        item = Item(name=new_item)
        db.session.add(item)
        db.session.commit()
    return index()
@app.route('/delete', methods=['POST'])
def delete():
    item_to_delete = request.form.get('item_to_delete')
    if item_to_delete:
        item = Item.query.filter_by(name=item_to_delete).first()
        if item:
            db.session.delete(item)
            db.session.commit()
    return index()
@app.route('/update', methods=['POST'])
def update():
    old_item = request.form.get('old_item')
    new_value = request.form.get('new_value')
    if old_item and new_value:
        item = Item.query.filter_by(name=old_item).first()
        if item:
            item.name = new_value
            db.session.commit()
    return index()
app.run(debug=True, host='0.0.0.0',port=5000)